import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import basicSsl from '@vitejs/plugin-basic-ssl'

export default defineConfig({
  plugins: [vue(), basicSsl()],
  server: {
    host: '0.0.0.0', // 允许外部访问
    port: 3000,
    https: true, // 启用HTTPS
    cors: true,
    strictPort: false,
    // 代理配置 - 解决开发环境跨域
    proxy: {
      '/audio': {
        target: 'http://192.168.1.11:81',
        changeOrigin: true,
        rewrite: (path) => path.replace(/^\/audio/, ''),
        secure: false, // 忽略 SSL 证书验证
        configure: (proxy, options) => {
          proxy.on('error', (err, req, res) => {
            console.log('Proxy error:', err)
          })
          proxy.on('proxyReq', (proxyReq, req, res) => {
            console.log('Sending Request to Target:', req.method, req.url)
          })
          proxy.on('proxyRes', (proxyRes, req, res) => {
            console.log('Received Response from Target:', proxyRes.statusCode, req.url)
          })
        }
      },
      '/query': {
        target: 'http://192.168.1.11:8000',
        changeOrigin: true,
        rewrite: (path) => path.replace(/^\/query/, ''),
        secure: false, // 忽略 SSL 证书验证
        configure: (proxy, options) => {
          proxy.on('error', (err, req, res) => {
            console.log('Proxy error:', err)
          })
          proxy.on('proxyReq', (proxyReq, req, res) => {
            console.log('Sending Request to Target:', req.method, req.url)
          })
          proxy.on('proxyRes', (proxyRes, req, res) => {
            console.log('Received Response from Target:', proxyRes.statusCode, req.url)
          })
        }
      }
    }
  },
  
  // 构建配置
  build: {
    outDir: 'dist',
    assetsDir: 'assets',
    sourcemap: false,
    minify: 'terser',
    rollupOptions: {
      output: {
        manualChunks: {
          vendor: ['vue', 'vue-router'],
          utils: ['axios', 'lodash']
        }
      }
    }
  },
  
  // 环境变量前缀
  envPrefix: 'VITE_'
})
